#Include G:\AHK\git-ahk-lib\util\event\MouseEvent.ahk
#Include G:\AHK\git-ahk-lib\lib\directOverlay\ShinsOverlayClass.ahk

#Include Button.ahk

ESC:: ExitApp()

w := 20, h := 800

x := floor(a_screenwidth * 0.6)
y := floor(a_screenheight * 0.1)

overlay := ShinsOverlayClass(x, y, w, h, 1, 0, 0,)
me := MouseEvent(overlay.hwnd)

sb := ScrollBarV(overlay, me, Rect(0, 0, w, h), 1)

refresh()

refresh() {
  overlay.BeginDraw()
  sb.Draw()
  overlay.EndDraw()
}


class ScrollBarV {

  __New(canvas, me, _rect, withArrow := false, arrowH := 20) {
    this.canvas := canvas, this.me := me, this._rect := _rect, this.withArrow := withArrow
    this.arrowH := withArrow ? 20 : 0
    this.scrollY := arrowH, this.scrollH := 90
    this.siderRect := Rect(0, 20, 20, this.scrollH)

    this.upArrow := Button(Rect(0, 0, 20, arrowH), '0x252525', '0x00AAAA')
    this.downArrow := Button(Rect(0, 0, 20, arrowH), '0x252525', '0x00AAAA')

    this.upArrow.OnEvent(Button.ON_PRESSED, (*) => this.addScrollY(-10))
    this.downArrow.OnEvent(Button.ON_PRESSED, (*) => this.addScrollY(10))

    this.RegisterEvents()
  }

  __Get(Key, Params) => this[Key]

  RegisterEvents() {
    if this.withArrow {
      this.me.Register(MouseEvent.Events.LBUTTON_DOWN, _onLD)
      this.me.Register(MouseEvent.Events.LBUTTON_UP, _onLU)
    }
    this.me.RegisterOnCondition(MouseEvent.Events.LBUTTON_DOWN,
      (point, *) => point.InBox(0, this.scrollY, this._rect.w, this.scrollH), _moveSlider
    )

    _onLD(point, *) {
      if this.upArrow._rect.PointIn(point)
        this.upArrow.OnPressed()
      else if this.downArrow._rect.PointIn(point, 0, this._rect.h - this.arrowH)
        this.downArrow.OnPressed()
    }

    _onLU(point, *) {
      this.upArrow.OnRelease(this.upArrow._rect.PointIn(point))
      this.downArrow.OnRelease(this.downArrow._rect.PointIn(point, 0, this._rect.h - this.arrowH))
      refresh()
    }

    _moveSlider(*) {
      CoordMode "Mouse", "Screen"
      MouseGetPos(, &py), dy := this.scrollY - py
      this.sliderPressed := 1
      While GetKeyState("LButton", "P") {
        MouseGetPos(, &ny)
        this.scrollY := this._ClampScrollY(ny + dy)
        refresh
      }
      this.sliderPressed := 0
      refresh
    }
  }

  _ClampScrollY(x) => Clamp(x, this.arrowH, this._rect.h - this.arrowH - 1 - this.scrollH)

  addScrollY(delta) {
    if this.scrollY < this.arrowH or this.scrollY > this._rect.h - this.arrowH - 1 - this.scrollH
      return
    this.scrollY := this._ClampScrollY(this.scrollY + delta)
    refresh
    KeyWait('Lbutton', 'T0.5')
    while GetKeyState('Lbutton', 'p') {
      this.scrollY := this._ClampScrollY(this.scrollY + delta)
      refresh
      Sleep 10
    }
  }

  DrawArrowBtn(overlay) {
    centerX := centerY := 10
    halfWidth := 10 // 2, thirdHeight := 10 // 3

    this.upArrow.Draw(overlay)

    overlay.DrawRectangle(1, 1, this._rect.w - 1, this.arrowH - 1, 0xFFFFFF)
    overlay.FillPolygon([
      [centerX - halfWidth, centerY + thirdHeight],
      [centerX + halfWidth, centerY + thirdHeight],
      [centerX, centerY - thirdHeight]
    ], 0xFFFFFF)

    this.downArrow.Draw(overlay, 0, this._rect.h - this.arrowH)

    overlay.DrawRectangle(1, this._rect.h - this.arrowH, this._rect.w - 1, this.arrowH - 1, 0xFFFFFF)
    overlay.FillPolygon([
      [centerX - halfWidth, this._rect.h - 20 + centerY - thirdHeight],
      [centerX + halfWidth, this._rect.h - 20 + centerY - thirdHeight],
      [centerX, this._rect.h - 20 + centerY + thirdHeight]
    ], 0xFFFFFF)
  }

  Draw() {
    overlay := this.canvas
    overlay.FillRectangle(0, 0, this._rect.w, this._rect.h, '0xff696969')

    if this.withArrow
      this.DrawArrowBtn(overlay)

    overlay.FillRectangle(0, this.scrollY, this._rect.w, this.scrollH,
      this.sliderPressed ? '0x6effff' : '0xc1ffff')
  }

}